//
//  SISSAbnormalAlertView.m
//  HomeTest
//
//  Created by 优房网 on 2017/4/12.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSAbnormalAlertView.h"
#import "UIView+frameAdjust.h"
#import "SISSRecommandPassengerCell.h"
#import "SISSRecommandHouseCell.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

static NSString *recommandPassengerCellReuseID = @"recommandPassengerCellReuseID";
static NSString *recommandHousingCellReuseID = @"recommandHousingCellReuseID";

@interface SISSAbnormalAlertView ()<UITableViewDataSource,UITableViewDelegate,SISSRecommandPassengerCellDelegate,SISSRecommandHouseCellDelegate>

/** 弹窗主内容view */
@property (nonatomic,strong) UIView   *contentView;
/** 弹窗标题 */
@property (nonatomic,copy)   NSString *title;
/* tableView */
@property (weak, nonatomic) UITableView *tableView;

/* selectIndexPath */
@property (assign, nonatomic) NSIndexPath *selectIndexPath;

/* 判断是否选择主推荐 */
@property (assign, nonatomic) BOOL isSelectMain;

@end
@implementation SISSAbnormalAlertView

- (instancetype)initWithTitle:(NSString *)title {
    
    if (self = [super init]) {
        self.title = title;
        
        self.isSelectMain = NO;
        
        // UI搭建
        [self setUpUI];
    }
    return self;
}



#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  [self.title isEqualToString:@"选择主推荐客源"] ?  self.passengerArr.count : self.houseArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.title isEqualToString:@"选择主推荐客源"]) {
        SISSRecommandPassengerCell *cell = [tableView dequeueReusableCellWithIdentifier:recommandPassengerCellReuseID forIndexPath:indexPath];
        cell.isChooseMain = YES;
        cell.delegate = self;
        cell.passengerResourceModel = self.passengerArr[indexPath.row];
        return cell;
    } else {
        SISSRecommandHouseCell *cell = [tableView dequeueReusableCellWithIdentifier:recommandHousingCellReuseID forIndexPath:indexPath];
        cell.isChooseMain = YES;
        cell.delegate = self;
        cell.houseResourceModel = self.houseArr[indexPath.row];
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
    if (self.selectIndexPath == indexPath) {
        
    }else {
        
        if ([self.title isEqualToString:@"选择主推荐客源"]) {
            
            self.passengerArr[self.selectIndexPath.row].isSelected = NO;
            self.passengerArr[indexPath.row].isSelected = YES;
        } else {
            self.houseArr[self.selectIndexPath.row].isSelected = NO;
            self.houseArr[indexPath.row].isSelected = YES;

        }
        
        self.isSelectMain = YES;
    }

    self.selectIndexPath = indexPath;
    
    [tableView reloadData];
}


- (BOOL)recommandHouseResourceCell:(SISSRecommandHouseCell *)cell checkButtonIsSelected:(BOOL)isSelected {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    self.houseArr[self.selectIndexPath.row].isSelected = NO;
    self.houseArr[indexPath.row].isSelected = YES;
    self.selectIndexPath = indexPath;
    [self.tableView reloadData];
    self.isSelectMain = YES;
    
    return NO;
}

- (BOOL)recommandPassengerCell:(SISSRecommandPassengerCell *)cell checkButtonIsSelected:(BOOL)isSelected {
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    self.passengerArr[self.selectIndexPath.row].isSelected = NO;
    self.passengerArr[indexPath.row].isSelected = YES;
    self.selectIndexPath = indexPath;
    [self.tableView reloadData];
    self.isSelectMain = YES;
    
    return NO;
}


#pragma mark - UI搭建
/** UI搭建 */
- (void)setUpUI{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    [UIView animateWithDuration:0.1 animations:^{
        self.alpha = 1;
    }];
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc]init];
    self.contentView.frame = CGRectMake((SCREEN_WIDTH - 285) / 2, (SCREEN_HEIGHT - 215) / 2, 285, 260);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 6;
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.contentView.bounds.size.width, 66)];
    titleLabel.backgroundColor = COLOR(0x41B4CD);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = self.title;
    [self.contentView addSubview:titleLabel];
    
    // 中间的tableView
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, titleLabel.maxY + 10, self.contentView.width, 160) style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 自动行高
    tableView.estimatedRowHeight = 100;
    tableView.rowHeight = UITableViewAutomaticDimension;
    [tableView registerClass:[SISSRecommandPassengerCell class] forCellReuseIdentifier:recommandPassengerCellReuseID];
    [tableView registerNib:[UINib nibWithNibName:@"SISSRecommandHouseCell" bundle:nil] forCellReuseIdentifier:recommandHousingCellReuseID];
    [self.contentView addSubview:tableView];
    self.tableView = tableView;

    
    UIView *horizontalLine = [[UIView alloc] initWithFrame:CGRectMake(0, tableView.maxY, self.contentView.width, 1)];
    horizontalLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:horizontalLine];
    // 取消按钮
    UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, tableView.maxY + 15, self.contentView.width * 0.5, 40)];
    [self.contentView addSubview:cancelButton];
//    cancelButton.backgroundColor = [UIColor colorWithHexString:@"d51619"];
    [cancelButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    cancelButton.tag = 100;
    [cancelButton addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *verticalLine = [[UIView alloc] initWithFrame:CGRectMake(cancelButton.maxX, cancelButton.minY - 15, 1, 65)];
    verticalLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:verticalLine];
    
    // 确定按钮
    UIButton *confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(cancelButton.maxX, cancelButton.minY, self.contentView.width * 0.5, 40)];
    [self.contentView addSubview:confirmButton];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
//    confirmButton.backgroundColor = [UIColor colorWithHexString:@"c8c8c8"];
    [confirmButton setTitleColor:COLOR(0x334A60) forState:UIControlStateNormal];
    [confirmButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    confirmButton.tag = 101;
    [confirmButton addTarget:self action:@selector(confirmButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    //------- 调整弹窗高度和中心 -------//
    self.contentView.height = confirmButton.maxY + 10;
    self.contentView.center = self.center;
    
}

#pragma mark - 弹出此弹窗
/** 弹出此弹窗 */
- (void)show{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
}

#pragma mark - 移除此弹窗
- (void)dismiss{
    [self removeFromSuperview];
}
#pragma mark - 取消 & 确定按钮按钮点击

- (void)confirmButtonClicked {
    
    
    if (!self.isSelectMain) {
        
        [SISSTools showSVErrorTitle:self.houseArr.count > 0 ? @"请选择主推荐房源" : @"请选择主推荐客源" andTime:2 andColor:YES];
        return;
    }
 
    if ([self.delegate respondsToSelector:@selector(abnormalAlertView:clickedConfirmButton:)]) {
        [self.delegate abnormalAlertView:self clickedConfirmButton:self.selectIndexPath.row];
    }
    [self dismiss];
}

- (void)cancelButtonClicked {
    
     [self dismiss];
}


@end
